--[[
	Copyright (c) 2012 Eyal Shilony

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local addonName, addon = ...
local shortcuts = LibStub("LibShortcuts-4.0")
local dropdown = CreateFrame("Frame", "LonnyMenuFrame", nil, "UIDropDownMenuTemplate")
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

local wipe = wipe
local tinsert = table.insert
local tremove = table.remove
local tsort = table.sort

local Menu = { }
local SystemMenu = {
	{
		priority = 1001,
		text = LOGOUT,
		notCheckable = 1,
		func = function()
			Logout()
			HideUIPanel(GameMenuFrame)
		end,
	},
	{
		priority = 1002,
		text = EXIT_GAME,
		notCheckable = 1,
		func = function() 
			Quit()
			HideUIPanel(GameMenuFrame)
		end,
	},
	{
		priority = 2000,
		text = "",
		notCheckable = 1,
		disabled = true,
	},
	{
		priority = 2003,
		text = L["Reload UI"],
		notCheckable = 1,
		func = function() 
			ReloadUI()
		end,
	},
}

local function insertShortcut(name, priority, title, icon)
	table.insert(Menu, {
		shortcut = name,
		priority = priority,
		text = title,
		icon = icon,
		notCheckable = 1,
		func = function() shortcuts:RunShortcut(name) end,
	})
end

local function buildMenu(self, name, priority)
	shortcut = shortcuts:GetShortcut(name)
	if shortcut and priority > 0 then
		insertShortcut(name, priority, shortcut.title, shortcut.icon)
		self.db.profile.shortcuts[name] = priority
	end
end

local function comp(a, b)
	return a.priority < b.priority
end

function addon:BuildMenu()
	wipe(Menu)
	
	for name, priority in pairs(self.db.profile.shortcuts) do
		buildMenu(self, name, priority)
	end
	
	if #Menu > 0 then
		table.insert(Menu, {
			priority = 1000,
			text = "",
			notCheckable = 1,
			disabled = true,
		})
	end
	
	for k, v in pairs(SystemMenu) do
		table.insert(Menu, v)
	end
	
	table.sort(Menu, comp)
	LonnyMenuFrame:Hide()
end

function addon:UpdateMenu(name, priority)
	self.db.profile.shortcuts[name] = priority
	self:BuildMenu()
end

function addon:Open(frame, button)
	local shortcut
	local isLeftButton = self.db.profile.mouseButton == 1 and button == "LeftButton"
	local isRightButton = self.db.profile.mouseButton == 2 and button == "RightButton"
	
	for i, v in pairs(self.db.profile.accessibility) do
		if not IsModifierKeyDown() and v.modifier == 4
		or IsAltKeyDown() and v.modifier == 1 
		or IsControlKeyDown() and v.modifier == 2 
		or IsShiftKeyDown() and v.modifier == 3 then
			shortcut = v.shortcut
			break
		end
	end
	
	if isLeftButton or isRightButton then
		shortcuts:RunShortcut(shortcut)
	else
		EasyMenu(Menu, dropdown, frame, 0, 0, "MENU")
	end
end